package com.gx.smart.building.ui.spare

import android.content.Intent
import android.view.ViewGroup
import com.blankj.utilcode.util.ToastUtils
import com.gx.smart.base.BaseMVVMActivityV3
import com.gx.smart.building.R
import com.gx.smart.building.databinding.ActivitySparePartBinding
import com.gx.smart.building.ui.spare.adapter.SpareAdapter
import com.gx.smart.building.ui.spare.model.SparePart
import com.gx.smart.building.ui.spare.model.SpareStockMessageEvent
import com.gx.smart.building.ui.spare.model.WarehouseMessageEvent
import com.gx.smart.building.ui.spare.viewmodel.SparePartViewModel
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.warehouse.v1.WarehouseDto
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.dto.SpareDto
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.dto.SpareRecordDto
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.dto.SpareStockDto
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.Calendar

class SparePartActivity : BaseMVVMActivityV3<SparePartViewModel>() {
    private lateinit var viewBinding: ActivitySparePartBinding
    private lateinit var adapter: SpareAdapter
    lateinit var item: WarehouseDto
    lateinit var spareStocks: Map<Int, SpareStockDto>
    lateinit var positionList: Map<Int, Int>
    override fun onBindViewModel(): Class<SparePartViewModel> = SparePartViewModel::class.java

    override fun onBindLayout(): Int = R.layout.activity_spare_part

    override fun getToolbarTitle(): String = "备件申请"

    override fun initContentView(root: ViewGroup) {
        viewBinding = ActivitySparePartBinding.inflate(layoutInflater, root, true)
        adapter = SpareAdapter(this)
        viewBinding.recyclerView.adapter = adapter
        adapter.add(SparePart(1))
        adapter.add(SparePart(2))
    }

    override fun initView() {
        registerEventBus()
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterEventBus()
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun messageEvent(event: WarehouseMessageEvent) {
        item = event.item
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun messageEvent(event: SpareStockMessageEvent) {
        spareStocks = event.item
        positionList = event.positionList
        adapter.notifyDataSetChanged()
    }


    override fun initData() {
        viewBinding.warehouseName.text = item.warehouseName

        viewBinding.save.setOnClickListener {
            save()
        }
    }

    fun save() {
        val description = viewBinding.dealDesc.text.toString().trim()
        val warehouseId = item.id
        val warehouseCode = item.warehouseCode
        val warehouseName = item.warehouseName
        val recordTime = Calendar.getInstance().timeInMillis.toString()
        if (!this::spareStocks.isInitialized) {
            ToastUtils.showLong("请选择备件")
            return
        }
        val spareRecords = spareStocks.map {
            val num = adapter.dataList[it.key].num
            val spare1 = SpareDto.newBuilder(it.value.spareDto)
                .build()
            SpareRecordDto.newBuilder()
                .setId(it.value.id)
                .setTenantNo(1)
                .setOperateNum(num)
                .setSpare(spare1)
                .build()
        }.toList()
        viewModel.insertSpareStockRecord(
            warehouseId, warehouseCode, warehouseName,
            recordTime, description, spareRecords
        ).observe(this) {
            if (it) {
                startActivity(Intent(this, ResultActivity::class.java))
                finish()
            }
        }
    }

}